package com.neusoft.elmboot.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.neusoft.elmboot.po.DailyTotal;
import com.neusoft.elmboot.po.Orders;

@Mapper
public interface OrdersMapper {

	@Insert("insert into orders(userId,businessId,orderDate,orderTotal,daId,orderState) values(#{userId},#{businessId},#{orderDate},#{orderTotal},#{daId},0)")
	@Options(useGeneratedKeys=true,keyProperty="orderId",keyColumn="orderId")
	public int saveOrders(Orders orders);
	
	public Orders getOrdersById(Integer orderId);
	public List<Orders> listOrdersByUserId(String userId);
	
	@Update("update orders set orderState=1,payDate=#{currentDate} where orderId=#{orderId}")
	public int updateOrderStatus(Integer orderId,String currentDate);
	
	@Update("update orders set comment=#{comment} where orderId=#{orderId}")
	public int saveComment(Orders orders);
	
	@Select("select comment from orders where orderId=#{orderId}")
	public String getComment(Integer orderId) ;
	
	public List<Orders> listOrdersByBusinessId(Integer businessId);
	
	@Select("select date(payDate) as date, sum(orderTotal)as amount\n" +
			"\t\tfrom orders where userId = #{userId}\n" +
			"\t\t  and STR_TO_DATE(payDate, '%Y-%m-%d') between DATE_SUB(STR_TO_DATE(#{currentDate}, '%Y-%m-%d'), INTERVAL 1 WEEK) and STR_TO_DATE(#{currentDate}, '%Y-%m-%d')\n" +
			"\t\tgroup by date(payDate) order by date(payDate) asc;")
	public List<DailyTotal> listWeeklyTotalById(String userId,String currentDate);

	@Select("select date(payDate) as date, sum(orderTotal)as amount\n" +
			"\t\tfrom orders where userId = #{userId}\n" +
			"\t\t  and STR_TO_DATE(payDate, '%Y-%m-%d') between DATE_SUB(STR_TO_DATE(#{currentDate}, '%Y-%m-%d'), INTERVAL 1 MONTH) and STR_TO_DATE(#{currentDate}, '%Y-%m-%d')\n" +
			"\t\tgroup by date(payDate) order by date(payDate) asc;")
	public List<DailyTotal> listMonthlyTotalById(String userId,String currentDate);
	
}
